package lol.oxguy3.droidpong;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.View.OnKeyListener;
import android.widget.Toast;

public class PongView extends View implements OnTouchListener, OnKeyListener {
	
	Context cxt;
	Paint paint;
	PongBall ball;
	PongPaddle paddleLeft, paddleRight;
	Random rand;
	boolean entitiesNeedInit;
	boolean gameRunning;
	long dontOpenTilXmas;

	public PongView(Context c) {
		super(c);
		setFocusableInTouchMode(true);
		setOnTouchListener(this);
		setOnKeyListener(this);
		
		cxt = c;
		paint = new Paint();
		paint.setAntiAlias(true);
		rand = new Random();
		
		ball = null;
		paddleLeft = null;
		paddleRight = null;
		
		entitiesNeedInit = true;
		gameRunning = false;
		dontOpenTilXmas = 0;
		
		Toast.makeText(cxt, "Press the Search button to start!", Toast.LENGTH_LONG).show();
		
		/*System.out.println("w:"
		+String.valueOf(findViewById(R.id.linearLayoutMain).
				getWidth())
		+" h:"+String.valueOf(findViewById(R.id.linearLayoutMain).getHeight()));*/
	}

	public boolean onTouch(View v, MotionEvent event) {
		if (gameRunning) {
				paddleLeft.setPosY((int)event.getY() - (paddleLeft.getHeight()/2));
				invalidate();
		} else {
			//Toast.makeText(cxt, "Game is paused; press the Search button to resume.", Toast.LENGTH_LONG).show();
		}
		return true;
	}
	
	@Override
	protected void onDraw(Canvas cvs) {
		if (entitiesNeedInit) {
			ball = new PongBall(cxt, this, this.getWidth()/2, this.getHeight()/2, /*rand.nextInt(20)-10, rand.nextInt(20)-10*/8+(rand.nextInt(4)-2),8+(rand.nextInt(4)-2)/**/, 0xffffffff, 5);
			paddleLeft = new PongPaddle(cxt, this, 10, this.getHeight()/2-50, 0, 0, 0xff000099, true, 100);
			paddleRight = new PongPaddle(cxt, this, this.getWidth() - 20, this.getHeight()/2-50, 6, 6, 0xff990000, false, 100);
			entitiesNeedInit = false;
		}
		//TODO un-comment the experimental double buffering
		////cvs.saveLayer(null, paint, Canvas.ALL_SAVE_FLAG);
		
		cvs.drawColor(0xff000000);
		
		/** move everything */
		if (gameRunning) {
			paddleRight.onMove();
			ball.onMove();
			paddleLeft.onMove();
		} /*else {
			paint.setColor(0xffffffff);
			cvs.drawText("Press the Search button to play/pause!", 25, 20, paint);
		}*/
		
		/** draw the line and scores */
		paint.setColor(0xff888888);
		cvs.drawRect(this.getWidth()/2-2, 0, this.getWidth()/2+2, this.getHeight(), paint);
		cvs.drawText(Integer.toString(paddleLeft.getPlayerScore()), (long)this.getWidth()/2-15, (long)this.getHeight(), paint);
		cvs.drawText(Integer.toString(paddleRight.getPlayerScore()), (long)this.getWidth()/2+5, (long)this.getHeight(), paint);
		
		/** draw the left paddle */
		paint.setColor(paddleLeft.getColor());
		cvs.drawRect(paddleLeft.getPosX(), paddleLeft.getPosY(), paddleLeft.getPosX()+paddleLeft.getWidth(), paddleLeft.getPosY()+paddleLeft.getHeight(), paint);
		
		/** draw the right paddle */
		paint.setColor(paddleRight.getColor());
		cvs.drawRect(paddleRight.getPosX(), paddleRight.getPosY(), paddleRight.getPosX()+paddleRight.getWidth(), paddleRight.getPosY()+paddleRight.getHeight(), paint);
		
		/** draw the ball */
		paint.setColor(ball.getColor());
		cvs.drawCircle(ball.getPosX(), ball.getPosY(), ball.getRadius(), paint);
		
		////cvs.restore();
		invalidate();
	}

	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if (keyCode==KeyEvent.KEYCODE_SEARCH&&System.currentTimeMillis()>=dontOpenTilXmas) {
			if (gameRunning) {
				pauseGame();
			} else {
				gameRunning=true;
			}
			dontOpenTilXmas = System.currentTimeMillis()+2000;
			invalidate();
		}
		
		return true;
	}
	
	public void pauseGame() {
		gameRunning = false;
		Toast.makeText(cxt, "Game Paused!", Toast.LENGTH_SHORT).show();
	}
	
}
